﻿@page "/"
@inject HttpClient Http
@inject IJSRuntime JSRuntime

@using Newtonsoft.Json
<div class="container">
    <div class="row">
        <div class="col-md-4">
            <label for="inputNumber" class="form-label">请选择需要的产品：</label>
            <select class="form-select" @bind="SelectedDeviceType">
                @foreach (string deviceType in GetNameList())
                {
                    <option value="@deviceType">@deviceType</option>
                }
            </select>
        </div>
        <div class="col-md-4">
            <label for="inputNumber" class="form-label">生产产品的机器数量：</label>
            <input type="number" id="inputNumber" class="form-control" @bind-value="MachineCount" placeholder="请输入数字" />
        </div>
        <div class="col-md-4">
            <label for="inputNumber" class="form-label"></label>
            <button class="btn btn-primary  mt-4" @onclick="OnBtnClick">开始量化</button>
        </div>
    </div>
</div>@if (forecasts == null)
{
    <p><em>选择后计算</em></p>
}
else
{
    <table class="tablex">
        <thead>
            <tr>
                <th>物品名称</th>
                <th>需求量</th>
                <th>生产设备</th>
                <th>配方表</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var (forecast, index) in forecasts.Select((item, i) => (item, i)))
            {
                <tr>
                    <td>@forecast.Name</td>
                    <td>@forecast.OutputPerMin</td>
                    <td>@forecast.ProductionEquipment</td>
                    <td>@forecast.Recipe</td>
                </tr>
            }
        </tbody>
    </table>
<p style="color: red;">说明：个人认为因游戏每天时间不固定，所以以单个生产设备为单位</p>
    <p style="color: red;">说明：保留一位小数，如果满速生产设备向上取整</p>
    <p style="color: red;">说明：配方表是表示单个建筑每分钟生产需求量，请自行计算传送带是否达到速度上限</p>
<p style="color: red;">注意：此工具目前极度不完善，如果发现错误之处，请发送邮件至 <span style="color: green;">419285289@qq.com。</span></p>
<p style="color: red;">作者能力有限，不会前端，请见谅。</p>

    @code {
    private List<NeedsCatalog> forecasts = new List<NeedsCatalog>();
    private string SelectedDeviceType { get; set; } = "木材";

    private int MachineCount { get; set; } = 1;


    private GameRecipeCalculator? grc;

    protected override async Task OnInitializedAsync()
    {
        var json = await Http.GetStringAsync(Http.BaseAddress + "GameRecipeGroup.json");



        grc = new GameRecipeCalculator(json);


    }
    private List<string> GetNameList()
    {
        if (grc==null)
        {
            return new List<string>() { "数据读取失败，请刷新" };
        }
        else
        {
            return grc.GetNameList();
        }
    }

    

  

    private async Task OnBtnClick()
    {
        if (MachineCount <= 0)
        {
            await JSRuntime.InvokeVoidAsync("alert", "请输入大于零的有效数字！");
            return;
        }
        else
        {
            // 处理有效数字输入的情况
            if (SelectedDeviceType == "")
            {
                await JSRuntime.InvokeVoidAsync("alert", "请选择需要制造的物品！");
                return;
            }

            

        }
        if (grc==null)
        {
            await JSRuntime.InvokeVoidAsync("alert", "程序加载不完全！");
            return;
        }

        if (grc.CalculateTotalCost(SelectedDeviceType, MachineCount, out string msg, out NeedsCatalogList re))
        {
            forecasts = re.NeedsCatalogs;
            return;
        }
        else
        {
            await JSRuntime.InvokeVoidAsync("alert", $"计算失败 原因是{msg}！");
            forecasts = new  List<NeedsCatalog>();
            return;
        }
    }
}
}
